﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Reflection;

namespace MEFDemo
{
    class Program
    {
        //导入对象使用
        [Import("chinese_hello")]
        public IPerson person { set; get; }


        static void Main(string[] args)
        {
            var program = new Program();
            program.MyComposePart();


            var strRes = program.person.SayHello("李磊");
            Console.WriteLine(strRes);

            Console.Read();
        }

        //宿主MEF并组合部件
        void MyComposePart()
        {
            var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());

            var container = new CompositionContainer(catalog);
            //将部件（part）和宿主程序添加到组合容器
            container.ComposeParts(this);
        }
    }
}
